package com.ns.scs.task.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.task.bo.ScheduledActuatorBO;
import com.ns.scs.task.bo.ScheduledActuatorPageBO;
import com.ns.scs.task.dto.ScheduledActuatorPageDto;
import com.ns.scs.task.entity.ScheduledActuator;
import com.ns.scs.task.service.IScheduledTaskActuatorService;
import com.ns.scs.task.vo.ScheduledActuatorVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 执行器-业务接口
 *
 * @author liao.wei
 * @since 2023/11/20
 */
@RestController
@RequestMapping("/actuator")
@RequiredArgsConstructor
@CrossOrigin
public class ScheduledActuatorController {

    private final IScheduledTaskActuatorService scheduledTaskActuatorService;

    /**
     * 新增
     *
     * @param bo 参数对象
     * @return RoleVo
     */
    @PostMapping("add")
    @ReqLog(module = "执行器模块", name = "增")
    public ResultOk add(@RequestBody ScheduledActuatorBO bo) {
        ScheduledActuator scheduledActuator = new ScheduledActuator();
        BeanUtils.copyProperties(bo, scheduledActuator);
        scheduledActuator.setSort(scheduledTaskActuatorService.count() + 1);
        return ResultOk.getSuccessData(scheduledTaskActuatorService.saveOrUpdate(scheduledActuator));
    }

    /**
     * 更新
     *
     * @param bo 参数对象
     * @return RoleVo
     */
    @PostMapping("update")
    @ReqLog(module = "执行器模块", name = "改")
    public ResultOk update(@RequestBody ScheduledActuatorBO bo) {
        ScheduledActuator scheduledActuator = new ScheduledActuator();
        BeanUtils.copyProperties(bo, scheduledActuator);
        return ResultOk.getSuccessData(scheduledTaskActuatorService.updateById(scheduledActuator));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "执行器模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(scheduledTaskActuatorService.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param dto 参数
     * @return Result
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "执行器模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO dto) {
        if (CollectionUtils.isNotEmpty(dto.getIds())) {
            return ResultOk.getByBoolean(scheduledTaskActuatorService.removeBatchByIds(dto.getIds()));
        } else {
            return ResultOk.getError("批量删除失败！");
        }
    }

    /**
     * 查询详情
     *
     * @param id 参数
     * @return Result
     */
    @GetMapping("getById")
    @ReqLog(module = "执行器模块", name = "查")
    public ResultOk getById(Long id) {
        return ResultOk.getSuccessData(scheduledTaskActuatorService.getById(id));
    }

    /**
     * 分页查询
     *
     * @param bo 参数
     * @return Result
     */
    @PostMapping("getList")
    @ReqLog(module = "执行器模块", name = "查-集合")
    public ResultOk getList(@RequestBody ScheduledActuatorPageBO bo) {
        ScheduledActuatorPageDto dto = new ScheduledActuatorPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<ScheduledActuatorVO> page = this.scheduledTaskActuatorService.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 查询执行器
     *
     * @return Result
     */
    @GetMapping("/options")
    @ReqLog(module = "执行器模块", name = "执行任务", authFlag = false)
    public ResultOk selectOptions() {
        return ResultOk.getSuccess();
    }
}